
//
//  BaseTableViewController.swift
//  Sina
//
//  Created by 邹志华 on 15/10/29.
//  Copyright © 2015年 zzh. All rights reserved.
//

import UIKit

class BaseTableViewController: UITableViewController {

    //当实现这个方法,并且给View设置值,不会从其他地方加载View (Xib storyboard)
    //view = UITableView
    /**
    在 loadView中如果我们设置View的值,就会使用设置的View
    super.loadView() 创建TableView
    
    */
    let userLogin = false
//    let userLogin = true
    
    
    override func loadView() {
        userLogin ? super.loadView() : setupVisitorView()
        
    }
    
    //创建访客视图
    func setupVisitorView()
    {
        
        
        
        let vistorView = VistorView()
        view = vistorView

        
        //设置代理
        vistorView.vistorViewDelegte = self
        
//        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        //设置导航栏
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewRegistClick")
        
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewLoginClick")
        
        if self is HomeTableViewController {
         //首页
            vistorView.startrptatopnAnimation()
         //监听应用退到后台,和进入前台
           NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            
             NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
            
        } else if self is MessageTableViewController {
            //消息
            vistorView.setupInfo("visitordiscover_image_message",message :"没有梦想何必远方")
            
        } else if self is DiscoverTableViewController {
            //发现
            vistorView.setupInfo("visitordiscover_image_message",message :"没有梦想何必远方?")
        } else if self is ProfileTableViewController {
            //我
            vistorView.setupInfo("visitordiscover_image_message",message :"没有梦想何必远方??")
        }
    
    }
    
    //MAKR: - 通知方法
    func didEnterBackground() {
        //暂停动画
        (view as! VistorView).pauseAnimation()
    }
    func didBecomeActive() {
        //继续动画
        (view as! VistorView).resumeAnimation()
    }

   
}

///扩展BaseTableViewController  实现VistorViewDelegte
extension BaseTableViewController: VistorViewDelegte{
    //MAKR - 代理方法
    func vistorViewRegistClick(){
        print(__FUNCTION__)
        //注册按钮
    }
    func vistorViewLoginClick(){
        print(__FUNCTION__)
        //登录按钮
        
        ///加载授权界面
        let controller = OauthViewController()
        
        
        
        presentViewController(UINavigationController(rootViewController: controller), animated: true, completion: nil)
        
        
        
    }
}

